package com.zlay.javafx.examples;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.zlay.javafx.examples.common.ExCommonUtils;
import com.zlay.javafx.examples.common.ExConstant;
import com.zlay.javafx.examples.common.ExLanguageUtils;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TitledPane;
import javafx.scene.image.Image;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;


/**
 * JavaFX App
 */
public class App extends Application {

	private static Logger log = Logger.getLogger(App.class);
	
    public static void main(String[] args) {
    	PropertyConfigurator.configure(ExCommonUtils.APP_HOME + "/conf/log4j.properties");
		log.info("STARTING...APP_HOME:" + ExCommonUtils.APP_HOME);
        launch();
    }
    
    @Override
    public void start(Stage stage) throws IOException {
        BorderPane pane = new BorderPane();
        pane.setTop(getHeader());
        pane.setCenter(getCenter());
        pane.setBottom(getFoot());
        var scene = new Scene(pane, ExConstant.WINDOWS_WIDTH, ExConstant.WINDOWS_HEIGHT);
        stage.setScene(scene);
        stage.setMinWidth(400);
        stage.setMinHeight(300);
        stage.setTitle(ExLanguageUtils.getMessage("App.Name"));
        stage.getIcons().add(new Image(App.class.getResourceAsStream("/images/appicon.png")));
        stage.show();
    }

    public Node getHeader() throws IOException {
    	MenuBar bar = new MenuBar();
		Menu file = new Menu(ExLanguageUtils.getMessage("Menu.File"));
		Menu view = new Menu(ExLanguageUtils.getMessage("Menu.View"));
		Menu help = new Menu(ExLanguageUtils.getMessage("Menu.Help"));
		bar.getMenus().addAll(file, view, help);
		
		MenuItem about = new MenuItem(ExLanguageUtils.getMessage("Menu.about"));
		help.getItems().addAll(about);
		return bar;
    }
    
    public Node getCenter() throws IOException {
    	BorderPane pane = new BorderPane();
    	pane.setLeft(getAccordion());
    	pane.setCenter(getMain());
		return pane;
    }
    
    public Node getAccordion() throws IOException {
    	Accordion acc = new Accordion();
//    	acc.setBorder(new Border(new BorderStroke(Paint.valueOf("C8C8C8"),Paint.valueOf("C8C8C8"),Paint.valueOf("C8C8C8"),Paint.valueOf("C8C8C8")
//    			,BorderStrokeStyle.SOLID,BorderStrokeStyle.SOLID,BorderStrokeStyle.SOLID,BorderStrokeStyle.SOLID
//    			,CornerRadii.EMPTY,null,Insets.EMPTY)));
    	acc.setPrefWidth(ExConstant.Accordion_WIDTH);
    	acc.setMinWidth(100);
    	acc.getPanes().addAll(
    			 new TitledPane(ExLanguageUtils.getMessage("Menu.Basics"),getBasicsButtons())
    			,new TitledPane(ExLanguageUtils.getMessage("Menu.layout"),getLayoutButtons())
    			,new TitledPane(ExLanguageUtils.getMessage("Menu.control"),getControlButtons())
    			);
    	ObservableList<TitledPane> paneList = acc.getPanes();
    	for (int i = 0; i < paneList.size(); i++) {
    		TitledPane temp = paneList.get(i);
//    		temp.setBorder(new Border(new BorderStroke(Color.TRANSPARENT, BorderStrokeStyle.NONE, CornerRadii.EMPTY, null)));
    		if(i==0) {
    			acc.setExpandedPane(temp);//默认展开第一个
    		}
    		VBox tempV = (VBox) temp.getContent();
    		tempV.setPadding(new Insets(0));//内边距
    		ObservableList<Node> buttonList = tempV.getChildren();
    		if(buttonList!=null&&buttonList.size()>0) {
    			for (int j = 0; j < buttonList.size(); j++) {
					Button tempB = (Button) buttonList.get(j);
					tempB.setPrefWidth(ExConstant.Accordion_WIDTH);
				}
    		}
		}
		return acc;
    }
    
    public Node getBasicsButtons() throws IOException {
    	VBox box = new VBox();
    	Button btn = new Button(ExLanguageUtils.getMessage("Tab.Homepage.Name"));
    	btn.setOnAction(new ExActionEventHandler(Action.homePage));
		return box;
    }
    
    public Node getLayoutButtons() {
    	VBox box = new VBox();
		return box;
    }
    
    public Node getControlButtons() {
    	VBox box = new VBox();
		return box;
    }
    
    public Node getMain() throws IOException {
    	TabPane tp1 = new TabPane();
//    	tp1.setBorder(new Border(new BorderStroke(Paint.valueOf("C8C8C8"), BorderStrokeStyle.SOLID, CornerRadii.EMPTY, null)));
    	tp1.getTabs().addAll(new Tab(ExLanguageUtils.getMessage("Tab.Homepage.Name"),getHomePage()));
		return tp1;
    }
    
    public Node getHomePage() {
    	VBox box = new VBox();
    	box.setPadding(new Insets(7));
    	var label = new Label("Hello!");
    	box.getChildren().addAll(label);
		return box;
    }
    
    public Node getFoot() {
    	var javaVersion = SystemInfo.javaVersion();
        var javafxVersion = SystemInfo.javafxVersion();
    	var label = new Label("Hello, The current version JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
		return label;
    }

}